package Thread2;

/**
 * @Author wangzhipeng
 * @Date 2020-08-21 11:12 上午
 * @Description volatile 实现了内存的可见型
 *  引用场景：一写多读————————————>一个线程改 另一个线程读
 */
public class VolatileDemo {
    private volatile static boolean b = true;  //不加volatile的话循环无法结束，即使b已经被主线称所更改

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("循环开始》》》》》");
                while (b) {
                    int a=1;
                }
                System.out.println("循环结束，线程运行完成");
            }
        });

        System.out.println("主线程开始跑了");
        t1.start();
        Thread.sleep(3000);
        b = false;
        System.out.println("主线程跑完了");

    }
}
